using UnityEngine;

namespace NodeCanvas.Actions
{
	[Category("✫ Utility")]
	[Description("Display a UI label on the agent's position if seconds to run is not 0, else simply logs the message")]
	[AgentType(typeof(Transform))]
	public class DebugAction : ActionTask
	{
		public string log;

		public float YOffset;

		public float secondsToRun;

		public bool actionReturn = true;

		private Texture2D _tex;

		private Texture2D tex
		{
			get
			{
				if (!_tex)
				{
					_tex = new Texture2D(1, 1);
					_tex.SetPixel(0, 0, Color.white);
					_tex.Apply();
				}
				return _tex;
			}
		}

		protected override string info
		{
			get
			{
				return ((!(secondsToRun > 0f)) ? "Log '" : "UI Log '") + log + "'" + ((!(secondsToRun > 0f)) ? string.Empty : (" for " + secondsToRun + " sec."));
			}
		}

		protected override void OnExecute()
		{
			if (secondsToRun <= 0f)
			{
				Debug.Log(log);
				EndAction(actionReturn);
			}
			base.useGUILayout = !string.IsNullOrEmpty(log);
		}

		protected override void OnUpdate()
		{
			if (base.elapsedTime >= secondsToRun)
			{
				EndAction(actionReturn);
			}
		}

		private void OnGUI()
		{
			if (!(Camera.main == null) && !string.IsNullOrEmpty(log) && !(base.agent == null))
			{
				Vector3 vector = Camera.main.WorldToScreenPoint(base.agent.transform.position + new Vector3(0f, YOffset, 0f));
				Vector2 vector2 = new GUIStyle("label").CalcSize(new GUIContent(log));
				Rect position = new Rect(vector.x - vector2.x / 2f, (float)Screen.height - vector.y, vector2.x + 10f, vector2.y);
				GUI.color = new Color(1f, 1f, 1f, 0.5f);
				GUI.DrawTexture(position, tex);
				GUI.color = new Color(0.2f, 0.2f, 0.2f, 1f);
				position.x += 4f;
				GUI.Label(position, log);
				GUI.color = Color.white;
			}
		}
	}
}
